﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MRS.DBL.DAL;
using MRSUtil;
using System.Reflection;

namespace MRS.DBL.BLL
{
    public class UnitMgr
    {
        static log4net.ILog _logger = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        private UnitDAL _dal = new UnitDAL();

        /// <summary>
        /// 是否存在该名称或者code
        /// </summary>
        public bool Exists(string unitName, string unitCode)
        {
            return _dal.Exists(unitName, unitCode);
        }

        /// <summary>
        /// 更新单位信息时检测是否已存在单位名称或者单位编码
        /// </summary>
        /// <param name="unitId"></param>
        /// <param name="unitName"></param>
        /// <param name="unitCode"></param>
        /// <returns></returns>
        public bool ExistsUnitNameOrCodeWhenUpdate(int unitId, string unitName, string unitCode)
        {
            return _dal.ExistsUnitNameOrCodeWhenUpdate(unitId, unitName, unitCode);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(MRS.DBL.Model.UnitModel model)
        {
            if (model == null)
            {
                _logger.Info("UnitMgr.Add: UnitModel is Null");
                return MRSConsts.Code_Failed;
            }
            if (Exists(model.UnitName, model.UnitCode)) //判断是否已存在
            {
                _logger.Info("UnitMgr.Add: UnitName or UnitCode Exists");
                return MRSConsts.Code_Exists;
            }
            return _dal.Add(model);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(MRS.DBL.Model.UnitModel model)
        {
            if (model == null)
            {
                _logger.Info("UnitMgr.Update: UnitModel is Null");
                return false;
            }
            if (ExistsUnitNameOrCodeWhenUpdate(model.UnitId,model.UnitName, model.UnitCode)) //判断是否已存在
            {
                _logger.Info("UnitMgr.Update: UnitName or UnitCode Exists");
                return false;
            }
            return _dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int UnitId)
        {
            if (UnitId < 1)
            {
                _logger.Info("UnitMgr.Delete:UnitId < 1 ");
                return false;
            }
            return _dal.Delete(UnitId);
        }
        /// <summary>
        /// 批量删除数据 UnitId 之间以逗号(,)隔开
        /// </summary>
        public bool DeleteList(string UnitIdlist)
        {
            if (string.IsNullOrEmpty(UnitIdlist)) 
            {
                _logger.Info("UnitMgr.DeleteList:UnitIdList is Null");
                return false;
            }
            return _dal.DeleteList(UnitIdlist);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public MRS.DBL.Model.UnitModel GetModel(int UnitId)
        {
            return _dal.GetModel(UnitId);
        }
        /// <summary>
        /// 获取所有单位列表
        /// </summary>
        /// <returns></returns>
        public DBL.Model.UnitModel[] GetUnitList()
        {
            return _dal.GetUnitList();
        }

        /// <summary>
        /// 分页查询单位信息
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <param name="totalPage"></param>
        /// <param name="model"></param>
        /// <param name="model2"></param>
        /// <param name="asc"></param>
        /// <returns></returns>
        public DBL.Model.UnitModel[] GetUnitListByPage(int pageIndex, int pageSize, out int totalCount, out int totalPage, Model.UnitModel model,Model.UnitModel model2,bool asc) 
        {
            UnitPageBLL _unitPageBLL = new UnitPageBLL();
            _unitPageBLL.CurPage = pageIndex;
            _unitPageBLL.PageSize = pageSize;
            _unitPageBLL.GetUnitInfoList(model, model2, asc);
            DBL.Model.UnitModel[] list = _unitPageBLL.GetPage();
            totalCount = _unitPageBLL.TotalCount;
            totalPage = _unitPageBLL.TotalPage;
            return list;
        }
    }
}
